package com.alog.common.code;

public class SyncConsumer extends Thread {

    private Object lock;


    public SyncConsumer(Object lock, String threadName){
        super(threadName);
        this.lock = lock;
    }


    @Override
    public void run() {
        while(true){
            synchronized (lock){
                if(Queue.getInstance().size() <= 0){
                    try {
                        lock.wait();
                        continue;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                String count = Queue.getInstance().poll();
                System.out.println(Thread.currentThread().getName() + ", poll " + count);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.notifyAll();
            }
        }
    }
}
